package com.begye.smartloan.apptest.outafter

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtOutRecordAfterBinding
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import com.begye.smartloan.apptest.huankuan.HuanKuanViewModel
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.tool.addTouch


@SuppressLint("CustomWelcomeScreen")
class OutRecordAfterActivity : AppCompatActivity() {

    private lateinit var _binding: AtOutRecordAfterBinding

    private val _huanKuanViewModel by lazy {
        ViewModelProvider(this)[HuanKuanViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtOutRecordAfterBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configUIState() {

        _huanKuanViewModel.onHuanKuanDetailLiveData.observe(this) {
            it?.let { result ->
                _binding.atOutRecordAfterDetail2.text =
                    result.optString("repayAmount", "0").toThousandUnitFormat()
                _binding.atOutRecordAfterDetail3.text =
                    result.optString("repayDate", "")
            }
        }
    }

    private fun configUIEvent() {

        _binding.atOutRecordAfterNar.initConfig(
            text = getString(R.string.out_record_text_7),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                finish()
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }
    }

    private fun configUIData(){
        _huanKuanViewModel.requestHuanKuanList()
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

